package org.LZQ.ShortLink.admin.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.LZQ.ShortLink.admin.common.biz.user.UserContext;
import org.LZQ.ShortLink.admin.common.convention.exception.ServiceException;
import org.LZQ.ShortLink.admin.common.convention.result.Result;
import org.LZQ.ShortLink.admin.domain.Group;
import org.LZQ.ShortLink.admin.mapper.GroupMapper;
import org.LZQ.ShortLink.admin.remote.RemoteService;
import org.LZQ.ShortLink.admin.remote.dto.req.ShortLinkRecycleBinPageReqDTO;
import org.LZQ.ShortLink.admin.remote.dto.resp.ShortLinkPageRespDTO;
import org.LZQ.ShortLink.admin.service.RecycleBinService;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 回收站管理接口实现层
 */
@Service
@RequiredArgsConstructor
public class RecycleBinServiceImpl  implements RecycleBinService {

    private final RemoteService remoteService;
    private final GroupMapper groupMapper;

    @Override
    public Result<Page<ShortLinkPageRespDTO>> pageRecycleBinShortLink(ShortLinkRecycleBinPageReqDTO requestParam) {
        LambdaQueryWrapper<Group> queryWrapper = Wrappers.lambdaQuery(Group.class)
                .eq(Group::getUsername, UserContext.getUsername())
                .eq(Group::getDelFlag, 0);
        List<Group> groupDOList = groupMapper.selectList(queryWrapper);
        if (CollUtil.isEmpty(groupDOList)) {
            throw new ServiceException("用户无分组信息");
        }
        requestParam.setGidList(groupDOList.stream().map(Group::getGid).toList());
        return remoteService.pageRecycleBinShortLink(requestParam.getGidList(), requestParam.getCurrent(), requestParam.getSize());
    }
}

